package com.ms.service.module.zone;

import com.ms.game.bank.CashChangeWay;
import com.ms.game.bank.db.BankDAO;
import com.ms.game.bank.db.CashLogModel;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.account.db.AccountModel;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.ms.service.module.zone.message.SRobotAddCashMessage;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.service.message.MessageAction;

public class RobotAddCashPointAsync extends CentralAsyncCommand{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6845640008362707487L;
	public GUID accountID;
	public GUID sessionID;
	public GUID playerID;
	public String playerName;
	public long cashChangeValue;
	
	public int messageType;
	private boolean isSuccess = false;
	private long cashAfterChange = 0l;
	private AccountModel robotAccount = null;
	
	@Override
	public void asyncExecute() throws Exception {
		long cashBeforeChange = 0l;
		robotAccount = AccountDAO.loadRobotAccountByAccountId(accountID.toString());
		if(robotAccount != null){
			cashBeforeChange = robotAccount.getCash();
			cashAfterChange = cashBeforeChange + cashChangeValue;
			GameUtility.writeCashUpdateConsoleLog("RobotAddCashPointAsync.asyncExecute()", robotAccount.getAccountId(), cashBeforeChange, cashChangeValue, cashAfterChange);
			isSuccess = BankDAO.UpdateAccountCashPoint(GUID.parseUUID(robotAccount.getAccountId()), cashAfterChange, false);
		}
		if(isSuccess){
			String orderId = GUIDFactory.createUUID(GUIDType.TYPE_CASH_POINT).toString();
			CashLogModel logModel = new CashLogModel();
			logModel.setOrderId(orderId);
			logModel.setAccountId(robotAccount.getAccountId());
			logModel.setPlayerId(playerID.toString());
			logModel.setCashBeforeChange(cashBeforeChange);
			logModel.setCashChangeValue(cashChangeValue);
			logModel.setCashAfterChange(cashAfterChange);
			logModel.setOpenId(robotAccount.getCooperatorAccountId());
			logModel.setChangeWay(CashChangeWay.ADD_ROBOT_ADD_CASH.getId());
			logModel.setChangeWayDesc(CashChangeWay.ADD_ROBOT_ADD_CASH.getDesc());
			getCurrentModuleServer().saveDbLog(logModel);			
		}
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		SRobotAddCashMessage msg = new SRobotAddCashMessage();
		if(robotAccount == null){
			msg.setErrorCode(ErrorCode.NO_ACCOUNT_DATA);
			MessageAction.sendMessage(sessionID, messageType, msg);
			return;
		}else if(!isSuccess){
			msg.setErrorCode(ErrorCode.DB_OPERATION_FAIL);
			MessageAction.sendMessage(sessionID, messageType, msg);
			return;
		}
		
		msg.setCashAfterChange(cashAfterChange);
		msg.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionID, messageType, msg);
	}

}
